package com.iweb.tmall.global;

import com.fasterxml.jackson.databind.JsonMappingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionAdvice {
    @ExceptionHandler(Exception.class)
    public ResponseData handleException(Exception e) {//e为被捕捉到的异常对象,但不知道具体是什么类型
        e.printStackTrace();
        log.error("发生异常",e);
        if (e instanceof CustomException) {  //如果异常对象e是我们自定义的异常类型
                CustomException ex = (CustomException)e;//向下转型
                return  new ResponseData(ex.getErrorEnum());
        } else if (e instanceof SQLException) {
                return  new ResponseData(ErrorEnum.DATABASE_ERROR);
        } else {
            return  new ResponseData(ErrorEnum.SYSYTEM_ERROR);
        }
    }
}
